Generating Library

Course- C IN LINUX >

The ability to generate interesting images dynamically to suit the situation is an enjoyable, challenging, and rewarding type of programming. We will be using homas Boutell’s GD C library which has been around for many years as an open systems project.

Here we create an image and print into it the value of the TEXT= part of the QUERY STRING and set our content type to image/gif. he gdImageGif function writes the binary image out to stdout which is the output stream instead of to a ile, so binary image data is sent back to the browser.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<gd.h>
#include<gdfonts.h>
#include<c_in_linux.h>


int main(int argc, char *argv[], char *env[])
{	int text=0, background=0, height=50, width=0;
	char value[255]="";
	gdimagePtr im_out=NULL;

	decode_value("TEXT=",(char*) &value,255);
	width=(strlen(value)*10)+5;
	im_out=gdImageCreate(width,height);
	backgrond=gdImageColorAllocate(im_out,255,0,255);
	text=gdImageColorAllocate(im_out,0,0,255);
	gdImageFilledRectangle(im_out,0,0, width-1, height-1, background);
	gdImageString(im_out, gdFontGetSmall();10,5,(unsigned char *)vlaue,   text);
	printf("Content_type:image/gif\n\n");
	gdImageGif(im_out,stdout);
	gdImageDestroy(im_out);
	gdImageDestroy(im_out);
	return 0;

}                  
                  

The program produces this output when called with cgi-bin/gdgraph1?TEXT=DavidHaskins While


Generating binary content

While this is not the most fancy or attractive exercise but does at least demonstrate the key principles involved in generating graphical output. With the GD library you can load existing images and generate them in many formats such as GIF, JPEG, PNG, WMBP and even create animated GIFS.

If you have used PHP development tools you will recognise the GD library functions as they are pretty well identical showing that PHP is a wrapper in this case around the same C library.

The text displayed here is using one of the internal fonts, Tiny, Small, Medium Bold, Large and Giant which are adequate for simple labelling but you can also use TrueType fonts for more attractive output.

Geometric drawing with lines or certain styles, filled and open polygons, and circles, arcs can be created. The main thing to remember is that the origin of an image is the TOP let hand corner which might seem unintuitive to anyone who has studied mathematics – quite why this is I have never discovered but can only imagine that the fffirst programmer to do anything in this area happened to not know about graphs in which we think of the origin x=0, y=0 as being at the bottom let hand.

Colours are speciied as RGB values in the range 0 to 255 so that white is 255,255,255 and red is 255,0,0. To work out ine-grained hues use a graphics tool like GNU GIMP which has colour pickers so you can ind out subtle RGB values. Palettes of colours from one image can be used in another and the closest colour in a palette requested or created if there is space for it to be allocated.